package 实验7;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.script.ScriptException;

public class Calculator extends JFrame implements ActionListener {
    JFrame jf;
    JTextField text;
    JButton Bt1, Bt2, Bt3, Bt4, Bt5, Bt6, Bt7, Bt8, Bt9, Bt0;
    JButton Add, Sub, Mul, Div, Equ, Del, Square, Sqrt, OpenParenthesis, CloseParenthesis;
    private JPanel jpNorth, jpEast, jpCenter;

    private StringBuilder expression = new StringBuilder(); // 存储计算式

    public Calculator() {
        jf = new JFrame();
        jf.setLayout(new BorderLayout(5, 5));

        // 创建按钮
        Bt1 = new JButton("1");
        Bt2 = new JButton("2");
        Bt3 = new JButton("3");
        Bt4 = new JButton("4");
        Bt5 = new JButton("5");
        Bt6 = new JButton("6");
        Bt7 = new JButton("7");
        Bt8 = new JButton("8");
        Bt9 = new JButton("9");
        Bt0 = new JButton("0");
        Add = new JButton("+");
        Mul = new JButton("*");
        Sub = new JButton("-");
        Div = new JButton("/");
        Equ = new JButton("=");
        Del = new JButton("C");
        Square = new JButton("x²");
        Sqrt = new JButton("√");
        OpenParenthesis = new JButton("(");
        CloseParenthesis = new JButton(")");

        // 排版
        jpNorth = new JPanel();
        jpCenter = new JPanel();
        jpEast = new JPanel();

        text = new JTextField(15);// 计算区域

        jf.setTitle("计算器");

        jpNorth.add(text);

        jpCenter.setLayout(new GridLayout(6, 4));// 网格布局
        jpNorth.setLayout(new GridLayout(1, 1));

        jpCenter.add(Add);
        jpCenter.add(Sub);
        jpCenter.add(Mul);
        jpCenter.add(Div);
        jpCenter.add(Bt1);
        jpCenter.add(Bt2);
        jpCenter.add(Bt3);
        jpCenter.add(Del);
        jpCenter.add(Bt4);
        jpCenter.add(Bt5);
        jpCenter.add(Bt6);
        jpCenter.add(Bt0);
        jpCenter.add(Bt7);
        jpCenter.add(Bt8);
        jpCenter.add(Bt9);
        jpCenter.add(Equ);
        jpCenter.add(Square);
        jpCenter.add(Sqrt);
        jpCenter.add(OpenParenthesis);
        jpCenter.add(CloseParenthesis);

        // 为按钮添加事件的处理
        Bt1.addActionListener(this);
        Bt2.addActionListener(this);
        Bt3.addActionListener(this);
        Bt4.addActionListener(this);
        Bt5.addActionListener(this);
        Bt6.addActionListener(this);
        Bt7.addActionListener(this);
        Bt8.addActionListener(this);
        Bt9.addActionListener(this);
        Bt0.addActionListener(this);
        Add.addActionListener(this);
        Sub.addActionListener(this);
        Mul.addActionListener(this);
        Div.addActionListener(this);
        Equ.addActionListener(this);
        Del.addActionListener(this);
        Square.addActionListener(this);
        Sqrt.addActionListener(this);
        OpenParenthesis.addActionListener(this);
        CloseParenthesis.addActionListener(this);

        jf.add(jpNorth, BorderLayout.NORTH);
        jf.add(jpCenter, BorderLayout.CENTER);

        jpCenter.setPreferredSize(new Dimension(300, 200));

        jf.setSize(500, 200);
        jf.setVisible(true);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        Calculator a = new Calculator();
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == Bt1) {
            expression.append("1");
        } else if (e.getSource() == Bt2) {
            expression.append("2");
        } else if (e.getSource() == Bt3) {
            expression.append("3");
        } else if (e.getSource() == Bt4) {
            expression.append("4");
        } else if (e.getSource() == Bt5) {
            expression.append("5");
        } else if (e.getSource() == Bt6) {
            expression.append("6");
        } else if (e.getSource() == Bt7) {
            expression.append("7");
        } else if (e.getSource() == Bt8) {
            expression.append("8");
        } else if (e.getSource() == Bt9) {
            expression.append("9");
        } else if (e.getSource() == Bt0) {
            expression.append("0");
        } else if (e.getSource() == Add) {
            expression.append("+");
        } else if (e.getSource() == Sub) {
            expression.append("-");
        } else if (e.getSource() == Mul) {
            expression.append("*");
        } else if (e.getSource() == Div) {
            expression.append("/");
        } else if (e.getSource() == Equ) {
            try {
                String result = calculate(expression.toString()); // 调用计算方法获取结果
                expression = new StringBuilder(result); // 更新计算式为结果
            } catch (ArithmeticException ex) {
                expression = new StringBuilder("除数不能为0"); // 处理除数为0的异常
            }
        } else if (e.getSource() == Del) {
            expression = new StringBuilder(); // 清空计算式
        } else if (e.getSource() == Square) {
            expression = new StringBuilder(calculate(expression.toString() + "^2")); // 调用平方运算符
        } else if (e.getSource() == Sqrt) {
            expression = new StringBuilder(calculate("sqrt(" + expression.toString() + ")")); // 调用开方运算符
        } else if (e.getSource() == OpenParenthesis) {
            expression.append("(");
        } else if (e.getSource() == CloseParenthesis) {
            expression.append(")");
        }

        text.setText(expression.toString()); // 在文本框中显示计算式
    }

    // 计算表达式并返回结果
    private String calculate(String expression) {
        try {
            return String.valueOf(eval(expression));
        } catch (Exception e) {
            return "非法计算式";
        }
    }

    // 使用Java脚本引擎计算表达式
    private double eval(String expression) throws ScriptException {
        Object result = new javax.script.ScriptEngineManager().getEngineByName("JavaScript").eval(expression);
        return Double.parseDouble(result.toString());
    }
}